from django.conf import settings
from django.http import HttpResponse, JsonResponse
# 表单
from django.views.decorators.csrf import csrf_exempt
import os

from DemoAIServer.requestImagetil import read_image, binary2cv
from .yolo import cvCNNImage
from .authUtil import auth, saveBianryFile


# 小文件大概是200k没问题，1.3M也没问题
@csrf_exempt
def yoloBinary(request):
    errormes = {'status': 2000}
    if auth(request):
        return JsonResponse(errormes)
    try:
        image = binary2cv(request)
        cvcnn = cvCNNImage()
        resData = cvcnn(image)
        # with open(number, 'rb') as f:
        #     image_data = f.read()
        # return HttpResponse(image_data, content_type="image/png")
        return JsonResponse(resData, safe=False)
    except IOError as err:
        print("recall error:{}".format(err))
        return JsonResponse(errormes)
